# -*- coding:utf-8 -*-
from flask import Flask, request, Response ,render_template
import cv2
import numpy as np
import base64
import os
import json
from yolo_util import getNodeInfo

app = Flask(__name__)
root_path = '/layout-service'

@app.route(root_path + '/')
def root():
    return render_template('index.html')

def base64_to_numpy(image_base64):
    image_bytes = base64.b64decode(image_base64)
    image_np = np.frombuffer(image_bytes, dtype=np.uint8)
    image_np2 = cv2.imdecode(image_np, cv2.IMREAD_COLOR)
    return image_np2


@app.route(root_path + "/layoutparse",methods=["POST"])
def layoutparse():
    imgData = request.form.get('imgData')

    img = base64_to_numpy(imgData)
    data = getNodeInfo(img)
    result = {'errCode': '0000','errMsg': '', 'result': {'data' : data }}
    return Response(json.dumps(result),  mimetype='application/json')

if __name__ == "__main__":
  app.run(host="0.0.0.0",port=8000)
